module key_led(
    input               sys_clk,
    input               sys_rst_n,
    
    input       [3:0]   key,
    output  reg [3:0]   led
    
);

//delay cnt
reg [23:0]  delay_cnt;

reg [1:0]   led_ctrl;


//计数器计数 计时为0.2s
always @(posedge sys_clk or negedge sys_rst_n)  begin
    if(!sys_rst_n)
        delay_cnt <= 24'd0;
    else
        if(delay_cnt < 24'd1000_0000)
            delay_cnt <= delay_cnt + 1'b1;
        else    
            delay_cnt <= 24'd0;
end

//每隔0.2s改变状态计数器
always @(posedge sys_clk or negedge sys_rst_n)   begin
    if(!sys_rst_n)
        led_ctrl <= 2'd0;
    else
        if(delay_cnt == 24'd1000_0000)
            led_ctrl <= led_ctrl + 1'b1;
        else
            led_ctrl <= led_ctrl;
end

//根据按键来改变各个状态下所点亮的LED
always @(posedge sys_clk or negedge sys_rst_n)   begin
    if(!sys_rst_n)
        led <= 4'b0000;
    else
        if(key[0] == 1'b0)
            case(led_ctrl)
                2'd0:   led <= 4'b1000;
                2'd1:   led <= 4'b0100;
                2'd2:   led <= 4'b0010;
                2'd3:   led <= 4'b0001;
            endcase
        else if(key[1] == 1'b0)
            case(led_ctrl)
                2'd0:   led <= 4'b0001;
                2'd1:   led <= 4'b0010;
                2'd2:   led <= 4'b0100;
                2'd3:   led <= 4'b1000;
            endcase
         else if(key[2] == 1'b0)
            case(led_ctrl)
                2'd0:   led <= 4'b1111;
                2'd1:   led <= 4'b0000;
                2'd2:   led <= 4'b1111;
                2'd3:   led <= 4'b0000;
            endcase
        else if(key[3] == 1'b0)
            led <= 4'b1111;
        else
            led <= 4'b0000;                  
end

endmodule